﻿namespace DimensionsHelper.LanguageService.Syntax;

public class ExpressionStatement : Statement
{
    private Expression _expression;

    internal ExpressionStatement(SyntaxNode? parent) :
        base(SyntaxKinds.ExpressionStatement, parent)
    {
        _expression = EmptyExpression.Default;
    }

    /// <summary>
    ///     默认的空语句，可用于初始化
    /// </summary>
    public static ExpressionStatement Default { get; } = new(null);

    /// <summary>
    ///     表达式语句中的单行表达式
    /// </summary>
    public Expression Expression => _expression;


    public override bool IsEmpty => Expression.IsEmptyExpression(Expression);


    public static bool IsDefault(ExpressionStatement statement)
    {
        return statement == Default || Expression.IsEmptyExpression(statement.Expression);
    }


    public ExpressionStatement Update(Expression expression)
    {
        SetChildField(ref _expression, Expression.IsEmptyExpression, expression);
        return this;
    }


    public override void AcceptVisitor<T>(SyntaxNodeVisitor<T> visitor)
    {
        Accept(visitor, Expression);
    }
}


public static partial class NodeFactory
{
    public static ExpressionStatement CreateExpressionStatement(SyntaxNode? parent, Expression expression)
    {
        ExpressionStatement statement = new(parent);
        return statement.Update(expression);
    }
}